package com.yami.shop.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.model.JiaoyouOrder;
import com.yami.shop.bean.model.RechargePackage;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.VipPackage;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.dao.JiaoyouOrderMapper;
import com.yami.shop.dao.UserMapper;
import com.yami.shop.dao.VipPackageMapper;
import com.yami.shop.service.JiaoyouOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.service.UserService;
import com.yami.shop.service.VipPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 订单表 充值或购买vip 服务实现类
 * </p>
 *
 * @author chajiu
 * @since 2021-07-14
 */
@Service
public class JiaoyouOrderServiceImpl extends ServiceImpl<JiaoyouOrderMapper, JiaoyouOrder> implements JiaoyouOrderService {

    @Autowired
    private UserService userService;

    @Autowired
    private VipPackageService vipPackageService;

    @Autowired
    private VipPackageMapper vipPackageMapper;

    @Autowired
    private UserMapper userMapper;


    private final Integer VIP_UNLOCK_COUNT = 10;

    @Override
    public JiaoyouOrder getByOrderNum(String orderNum) {
        JiaoyouOrder order = getOne(Wrappers.lambdaQuery(new JiaoyouOrder())
                .eq(JiaoyouOrder::getOrderNumber, orderNum));
        return order;
    }

    @Override
    public void doRecharge(JiaoyouOrder order) {
    //处理充值
        userService.addDiamond(order.getUserId(), order.getDiamondNum());
    }

    @Override
    public void doVipPay(JiaoyouOrder order) {
    //处理vip
        VipPackage p = vipPackageService.getById(order.getPackageId());
        String userId = order.getUserId();
        Date now = new Date();
        /*if(userService.getById(userId).getIsVip() == 1){
            throw new YamiShopBindException("已经是VIP，无法购买VIP");
        }*/
        User user = new User();
        p.getDayCount();
        user.setUserId(userId);
        if (userService.getById(userId).getIsVip() == 0){
            user.setVipStartTime(now);
            user.setVipEndTime(DateUtil.offsetDay(now,p.getDayCount()));
        }else {//是VIP的话说明是续充
            Date endTime = userMapper.selectById(userId).getVipEndTime();
            user.setVipEndTime(DateUtil.offsetDay(endTime,p.getDayCount()));
        }
        user.setIsVip(1);
        user.setTodayUnlockSurplus(VIP_UNLOCK_COUNT);
        if(userService.getById(userId).getIsFirstVip() == 0){
            user.setIsFirstVip(1);
            if (userService.getById(userId).getUppInvitatioCode() != null && userService.getById(userId).getUppInvitatioCode().equals("")){
                //被邀请的朋友首次购买VIP，邀请人可以享受48小时内解密6个微信号时间和解密数量
                userMapper.uppCodeRepeatVip(userService.getById(userId).getUppInvitatioCode());
                if (userService.getById(userId).getSex().equals("M")){//当前用户是男生
                    if (userMapper.uppJudge(userService.getById(userId).getUppInvitatioCode()) != null){//上级用户性别不等于null
                        if (userMapper.uppJudge(userService.getById(userId).getUppInvitatioCode()).equals("F")){//如果上级是女生的话
                            //把rmb都反给女生
                            User user1 = userMapper.selectById(userId);//当前用户的信息
                            User user2 = userMapper.selectOne(new QueryWrapper<User>().eq("invitatio_code",user1.getUppInvitatioCode()));//上级用户信息
                            VipPackage vips = vipPackageMapper.selectOne(new QueryWrapper<VipPackage>().eq("id",order.getPackageId()));
                            user2.setWallet(user2.getWallet() + vips.getPrice());
                            userMapper.updateById(user2);
                        }

                    }
                }

            }
        }else if(userService.getById(userId).getIsFirstVip() == 1){
            if (now.before(userService.getById(userId).getVipEndTime())){
                //首次VIP续充
                user.setIsFirstVip(2);
            }
        }
        userService.updateById(user);
    }
}
